#include<bits/stdc++.h>
using namespace std;
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m;
    scanf("%d%d",&n,&m);
    int a[n+1],b[m+1];
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=m;i++){
        scanf("%d",&b[i]);
    }
    int k=0;
    while(pow(2,k)<n){
        k++;
    }
    bool f[(int)(pow(2,k))+1][k+1];
    char kkkk;
    for(int i=k-1;i>=0;i--){
        getchar();
        for(int j=1;j<=pow(2,i);j++){
            scanf("%c",&kkkk);
            if(kkkk=='1'){
                f[i][j]=1;
            }
            else{
                f[i][j]=0;
            }
        }
    }
    int t,ttt[4],c[n+1],d[n+1];
    scanf("%d",&t);
    for(int tt=1;tt<=t;tt++){
        scanf("%d%d%d%d",&ttt[0],&ttt[1],&ttt[2],&ttt[3]);
        for(int i=1;i<=n;i++){
            c[n+1]=a[n+1]^ttt[i%4];
        }
        int ans=0;
        for(int i=1;i<=m;i++){
            int tmp=c[i],tmp2=k;
            for(int j=1;j<=tmp;j++){
                d[j]=j;
            }
            while(tmp>1){
                tmp2--;
                for(int j=1;j<=tmp/2;j++){
                    if((f[tmp2][j]==true&&c[j*2]<k-tmp2)||(f[tmp2][j]==false)&&c[j*2-1]>=k-tmp2){
                        d[j]=j*2-1;
                    }
                    else{
                        d[j]=j*2;
                    }
                }
            }
            ans^=(i*d[1]);
        }
        printf("%d\n",ans);
    }
    fclose(stdin);
    fclose(stdout);
    return 0;
}
